﻿using System;
using System.Windows.Input;

namespace UploadClient.Framework
{
    public class DelegateCommand : ICommand
    {
        #region Fields (2)

        private readonly Func<object, bool> canExecuteCommand;
        private readonly Action<object> executeCommand;

        #endregion Fields

        #region Constructors (2)

        public DelegateCommand(Action<object> executeCommand, Func<object, bool> canExecuteCommand)
        {
            this.executeCommand = executeCommand;
            this.canExecuteCommand = canExecuteCommand;
        }

        public DelegateCommand(Action<object> executeCommand) : this(executeCommand, null) { }

        #endregion Constructors

        // Events (1) 

        #region ICommand Members

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        #endregion

        #region Methods (2)

        // Public Methods (2) 

        public bool CanExecute(object parameter)
        {
            bool result = false;

            result = (canExecuteCommand == null) ? true : canExecuteCommand(parameter);

            return result;
        }

        public void Execute(object parameter)
        {
            executeCommand(parameter);
        }

        #endregion Methods
    }
}
